package com.zoro.barn.business.service.system.business.roleuser.controller;

import com.zoro.barn.business.service.system.business.roleuser.service.BarnRoleUserService;
import com.zoro.barn.business.service.system.business.roleuser.entity.BarnRoleUser;
import com.zoro.barn.business.service.system.business.user.controller.dto.AddRoleIdsDto;
import com.zoro.barn.business.service.system.business.user.entity.BarnUser;
import com.zoro.barn.business.service.system.business.user.exceptions.UserNotExistException;
import com.zoro.barn.business.service.system.business.user.service.BarnUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/role-user")
public class BarnRoleUserController {
    private final BarnUserService barnUserService;
    private final BarnRoleUserService barnRoleUserService;

    public BarnRoleUserController(BarnUserService barnUserService, BarnRoleUserService barnRoleUserService) {
        this.barnUserService = barnUserService;
        this.barnRoleUserService = barnRoleUserService;
    }


    /**
     * 向用户添加角色
     *
     * @param dto {@link AddRoleIdsDto}
     */
    @RequestMapping("/addRoleIds")
    public void addRoleIds(@RequestBody AddRoleIdsDto dto) {

        BarnUser data = this.barnUserService.getById(dto.getPid());
        if (data == null) {
            throw new UserNotExistException(dto.getPid());
        }
        this.barnRoleUserService.addRole(dto.getPid(), dto.getRoleIds());

    }

    /**
     * 删除用户与角色关系
     *
     * @param dto role and user's id
     */
    @RequestMapping("delete")
    public void delete(@RequestBody BarnRoleUser dto) {

        if (StringUtils.isAnyBlank(dto.getRoleId(), dto.getUserId())) {
            return;
        }
        this.barnRoleUserService.delete(dto.getRoleId(), dto.getUserId());

    }
}
